#ifndef STANDARDCHANMANAGER_H
#define STANDARDCHANMANAGER_H

#include <QObject>
#include <QStringList>
#include <QTimer>
#include <QDomElement>
#include "chanmanager.h"
#include "chanagents/agentfactory.h"
#include "requestmanager.h"
#include "contentpipe.h"

namespace Wishmaster {

	class CaptchaFetcherFactory;
	class WISHMASTER_EXPORT StandardChanManager : public ChanManager
	{
		Q_OBJECT
	public:
                StandardChanManager(MainWindow* parent, ThreadStorage* threadStorage);
		virtual ~StandardChanManager();

		bool initialize();
		bool shutdown();

		void setImageView(ImageViewForm* imageview);

		virtual bool requestFavicon(const QString& chanName);
		virtual bool requestBoardIndex(const QString& chanName, const QString& boardId, int page);
		virtual bool requestThread(const ThreadId& threadId);
		virtual bool requestThumbnail(const PostId& postId, int attachmentIndex, const QUrl& url);
		virtual bool requestPic(const PostId& postId, int attachmentIndex);
		virtual bool downloadFile(const PostId& postId, int attachmentIndex, const QString& filename);
		virtual bool requestCaptcha(const ThreadId& threadId);
		virtual bool addPost(const ThreadId& threadId, const PostDescriptor& post, const QString& captcha);
		virtual bool createThread(const QString& chanName, const QString& boardId, const PostDescriptor& post, const QString& captcha);

		void setFilterList(const FilterListPtr& list);
		FilterListPtr filterList() const;

		void refresh(bool ignoreOverrides, bool indexOnly);
		virtual void updateContent(const QString& chanName, const QString& boardId);
		virtual void updateContent(const PostId& postId, int attachmentIndex);
		virtual void updateAttachment(const PostId& postId, int attachmentIndex);
		void collectGarbage();

		void showPicture(const PostId& postId, int attachmentIndex);

		bool loadChanList(const QString& filename, AgentFactory* agentFactory);
		bool loadChanList(const QByteArray& data, AgentFactory* agentFactory);

		FilterListPtr globalFilterList();

		void setChanlistFilename(const QString& filename);

		virtual Taskhub* taskhub();
		virtual Connector* connector();
		virtual ChanList* chanlist() const;
		virtual ChanWatcher* chanwatcher() const;

		void setThreadStorage(ThreadStorage* storage);
		ThreadStorage* threadStorage() const;

		virtual Wishmaster2::Network::RequestManager* requestManager() const;

	public slots:
		void swap();
		void newMessages(const PostId& thread);

	protected slots:
		void refreshImageView(const QImage& image, const QByteArray& postHid);
		void refreshImageView(const QByteArray& arr, const QByteArray& postHid);
		void refreshImageView(const ExtraContainer& extra, const QByteArray& postHid);

		void progressUpdate(int total);
		void chanIconChanged(const QString& host);
		void displayNewMsgNotification();

		void postingDone(const ThreadId& threadId);
		void slot_captcha(const ThreadId& threadId, const QByteArray& data);

	private:
		bool parseChanNode(const QDomNode& node, AgentFactory* agentFactory);
		void loadBoard(Chan* parent, ChanAgent* ag, const QString& parser, const QDomNode& node, const QString& timestampFormat);
		bool fillBoardConstraintsField(const QString& field, const QString& value,
									   BoardConstraints& constraints);
		void parseArgs(const QDomElement& n, Chan* chan);
		void showAttachment(const PostId& postId, int index);

		QStringList allChanNames() const;

		ImageViewForm* m_imageView;
		ThreadStorage* m_threadStorage;
		MainWindow* m_view;
		KeymapManager m_keymapManager;
		Extractor* m_extractor;
		FilterListPtr m_filterList;
		QString m_chanlistFile;
		Taskhub* m_taskhub;
		ChanWatcher* m_watcher;
		Wishmaster2::Network::RequestManager* m_requestManager;
		QTimer m_newMsgTimer;
		QList<PostId> m_newMsgThreads;
		ChanList* m_chanList;
		AgentFactory* m_agentFactory;
		CaptchaFetcherFactory* m_captchaFetcherFactory;
		ContentPipe* m_contentPipe;
	};

} // namespace Wishmaster

#endif // STANDARDCHANMANAGER_H
